<?php
class Classes{
	public function getClass($id){
		$DB = new DB();
		$sql = "SELECT * FROM class where grade_id = '$id'";
		return $DB->fetchAll($sql);
	}
	
	public function getClassByTeacher($id){
		$DB = new DB();
		$sql = "SELECT * FROM class where teacher_id = '$id'";
		
		return $DB->fetchRow($sql);	
	}
	
	public function addClass($title,$grade){
		$DB = new DB();
		$sql = "INSERT INTO `class` VALUES (:id,:teacher,:title,:grade)";
		$field = array(':id' => '',
					   ':teacher' => '',
					   ':title' => $title,
					   ':grade' => $grade
					  );	
		return $DB->updateDB($sql,$field);				  
	}
	
	public function getClassById($id){
		$DB = new DB();
		$sql = "SELECT * FROM class where id = '$id'";
		
		return $DB->fetchRow($sql);		
	}
	
	public function getAllClasses(){
		$DB = new DB();
		$sql = "SELECT c.*,u.username as teacher,g.title as grade 
				FROM class c
				LEFT JOIN user u ON c.teacher_id = u.id
				LEFT JOIN grade g ON g.id = c.grade_id
				
				";
		
		return $DB->fetchAll($sql);			
	}
	
	public function editTeacher($class_id,$teacher_id){
		$DB = new DB();
		$sql = "UPDATE `class` set teacher_id='$teacher_id' where id='$class_id'";
		
		$field = array();
		return DB::updateDB($sql,$field);
	}
	public function removeTeacher($teacher_id){
		$DB = new DB();
		$sql = "UPDATE `class` set teacher_id='' where teacher_id='$teacher_id'";
		$field = array();
		return DB::updateDB($sql,$field);
	}	
}
?>